$(function(){
	layui.use(['form','tree','util'],function(){
		 var form = layui.form
		  ,layer = layui.layer
		  ,tree = layui.tree
		  ,util = layui.util;
		 
		 $.ajax({
			 type : 'get'
			 ,dataType : 'json'
			 ,url : access_url+'/cache/loadcachedata/getdictsbypcode?parent_code=1000'
			 ,success : function(data){
				console.log("data",data);
				if(data.code != 200){
					layer.msg(data.msg, {icon: 5});
					return;
				}
				
				var dictlist = data.retlist;
				
				var dictLen = dictlist.length;
				
				var roleLimitHtml = '';
				
				for (var i = 0; i < dictLen; i++) {
					var dict = dictlist[i];
					var htmlModel = '<option value="{word_value}">{word_mean}</option>'
					var opHtml = htmlModel.format(dict);
					roleLimitHtml += opHtml;
				}
				
				$('#role_data_limit').append(roleLimitHtml);
				
				form.val('role-form');
		 	 }
		 })
		 
		 $.ajax({
			 type : 'get'
			 ,url : access_url+'/cache/loadcachedata/getmenutreefromcache'
			 ,dataType:'json'
			 ,success : function(data){
				 if(200!=data.code){
					layer.msg(data.msg, {icon: 5});
				 	return;
				 }
				 
				 var menuTree = data.retlist;
				 
				 tree.render({
					 elem: '#menuTree'
				     ,data: menuTree
				     ,id: 'menuTree'
				     ,showCheckbox: true  //是否显示复选框
				 });
			 }
		 })
		  
		 //点击保存按钮
		 util.event('lay-demo',{
			 saveRole : function(othis){
				 var formData = form.val('role-form');
				 
				 console.log("fromData",formData);
				 var checkedData = tree.getChecked('menuTree');
				 
				 console.log("checkedData",checkedData);
				 
				 var role_name = formData.role_name;
				 var role_data_limit = formData.role_data_limit;
				 var role_level = formData.role_level;
				 var role_code = formData.role_code;
				 
				 var menus = [];
				 
				 if(!isNull(checkedData)){
					 
					 menus = revertMenuNodeToArray(checkedData);
					 
					 console.log("endMenus",menus);
				 }
				 
				 layer.load(2);
				 $.ajax({
					 type : 'post'
					 ,dataType : 'json'
					 ,url : access_url+'/system/systemmanage/addrole'
					 ,contentType : 'application/json;charset=UTF-8'
					 ,data : JSON.stringify({
						 role_name : role_name
						 ,role_data_limit : role_data_limit
						 ,role_level : role_level
						 ,role_code : role_code
						 ,menus : menus
					 })
					 ,success : function(data){
						layer.closeAll('loading');
						if(data.code != 200){
							layer.msg(data.msg, {icon: 5});
							return;
						}
						parent.layer.alert(data.msg,{icon:6});
						parent.loadTable();
						parent.layer.closeAll('iframe');
					 }
				 	 ,error : function(){
				 		layer.closeAll('loading');
				 		layer.msg('保存失败,请联系管理员', {icon: 5});
				 	 }
				 })
			 }
		 })
	})
})


function revertMenuNodeToArray(checkedData){
	var menus = [];
	
	var checkedLen = checkedData.length;
	 
	for (var i = 0; i < checkedLen; i++) {
		var menu_id = checkedData[i].id;
		
		var menuNode = `{"menu_id":"${menu_id}"}`;
		
		menus.push(JSON.parse(menuNode));

		var children = checkedData[i].children;
		
		if(!isNull(children)){
			var menuArray = revertMenuNodeToArray(children);
			
			console.log("menuArray",menuArray);
			menus = menus.concat(menuArray);
			console.log("menus",menus);
		}
	}
	return menus;
	
}
